package com.example.bootrabbitmq.direct;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.PublisherCallbackChannel;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
@RabbitListener(queues = "QueueA")
public class DirectQueueA1Consumer {
    @RabbitHandler
    public void process(String content, Channel channel, Message message) throws IOException {
        log.info("A1-接收处理队列A当中的消息： " + content);
        System.err.println(channel.toString());
        System.err.println(message.toString());


        String msgId = (String) message.getMessageProperties()
                .getHeaders().get(PublisherCallbackChannel.RETURNED_MESSAGE_CORRELATION_KEY);
//            System.out.println(2 / 0);
        log.info("A1-接收处理队列A当中的消息标志ID：{} ", msgId);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        if (message.getMessageProperties().getRedelivered()) {
            log.error("消息已重复处理失败,拒绝再次接收...");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false); // 拒绝消息
        } else {
            log.error("消息即将再次返回队列处理...");
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }


//————————————————
//        版权声明：本文为CSDN博主「supercmd」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//        原文链接：https://blog.csdn.net/supercmd/article/details/93189617

    }
}
